package com.ddxz.tool.core.security;

import com.ddxz.tool.core.convert.StringUtil;
import com.ddxz.tool.core.crypto.SecureUtil;
import com.ddxz.tool.core.crypto.asymmetric.KeyType;
import com.ddxz.tool.core.crypto.asymmetric.RSA;
import com.ddxz.tool.core.crypto.symmetric.AES;
import com.ddxz.tool.core.crypto.symmetric.DES;

import java.io.File;
import java.io.InputStream;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/20
 */

public class SecurityUtil {
    private static final String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAO3fQBw7wJIB/NJrkvyKRWt96EfpoXD0rIvzk3M4B245+c9xuIAYuMtYQo6Q6GRiuVIB9Tmz6lrc5BOyCl8KRVWbnp0RCHyxqMhHi7IZuG/T8J289NlLmjew/piL1N6ZZnjjf5A2NqM3oxIJOZNjzpmhH2OZNwm1XiQ1NBYOi/qpAgMBAAECgYAt/RgZfUnlUKIPEoI39gmm1xBV+urd+kfzxNNQltgE7QuJmjxn85+inL09b/GmGtomaz98ePbJu2QTFLxhlIxDueUNuZWp+daDfftV/gaCKJU0sZ/t22OtwegpT3NwcAFx8c74A0k3Sera34R+adUL/ALnQXff6gbMPjw0LU/LCQJBAPpzGlimfeuM2x/F/QwAceGkqMtKyR7kUqN6iux4Bdq8e2wrZwlLYmDsQwDnQ8vujlrFunFXB9hFPmF/nbf1RyUCQQDzJMpNOx7mGl0txsdSOMVsO6qdim78zh3qmBoJLaGWfWuQoF2KR2dyWITaxfuJTGoDGiSOioZYkn4Yy4Zrn0A1AkAS+kHW0dbc0HGSOMhx7l6ywG1h6irPQnHYnWKFhJq89mdLzQYjuCieO4jgOqXygAMdcbos+HFJvAV8EdV7qQRZAkEAl4ChHaZtBmv/C3sEz9LYESzMjwWnSpcURQ0pi/aG7UKRN7j1ECiV6MYHYlpi9ZZLqEst5DOkqqP9Nm37ACxm0QJACfN90x3r+wFR+MJ9BS5yXUNcfRRpcPBOD7D4QrzOMkWUh3QkuaQZSESpud7UUt0PAfFfTFLx4XQyeY9CqdrYTg==";
    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDt30AcO8CSAfzSa5L8ikVrfehH6aFw9KyL85NzOAduOfnPcbiAGLjLWEKOkOhkYrlSAfU5s+pa3OQTsgpfCkVVm56dEQh8sajIR4uyGbhv0/CdvPTZS5o3sP6Yi9TemWZ443+QNjajN6MSCTmTY86ZoR9jmTcJtV4kNTQWDov6qQIDAQAB";
    private static final String AES128_KEY = "OGu0gkVrcDFp6ScZ+D54PQ==";
    private static final String AES192_KEY = "A9v8cgKvSmujiYuxK9FufvrMitQ1eh0t";
    private static final String AES256_KEY = "rFBCFPc0Lmpwoj8433kqgw9OsN+as8xUp6lHqEM1M20=";
    private static final String DES_KEY = "GoVngLmDV38=";

    private static final RSA R_S_A = SecureUtil.rsa(PRIVATE_KEY, PUBLIC_KEY);
    private static final AES A_E_S = SecureUtil.aes(Base64Util.decode(AES128_KEY));
    private static final AES AES_192 = SecureUtil.aes(Base64Util.decode(AES192_KEY));
    private static final AES AES_256 = SecureUtil.aes(Base64Util.decode(AES256_KEY));
    private static final DES D_E_S = SecureUtil.des(Base64Util.decode(DES_KEY));

    public static String rsaEncode(String data) {
        return Base64Util.encode(R_S_A.encrypt(data, KeyType.PublicKey));
    }

    public static String rsaDecode(String security) {
        return StringUtil.fromBytes(R_S_A.decrypt(Base64Util.decode(security), KeyType.PrivateKey));
    }

    public static String aesEncode(String data) {
        return Base64Util.encode(A_E_S.encrypt(data));
    }

    public static String aesDecode(String data) {
        return StringUtil.fromBytes(A_E_S.decrypt(Base64Util.decode(data)));
    }

    public static String aes192Encode(String data) {
        return Base64Util.encode(AES_192.encrypt(data));
    }

    public static String aes192Decode(String data) {
        return StringUtil.fromBytes(AES_192.decrypt(Base64Util.decode(data)));
    }

    public static String aes256Encode(String data) {
        return Base64Util.encode(AES_256.encrypt(data));
    }

    public static String aes256Decode(String data) {
        return StringUtil.fromBytes(AES_256.decrypt(Base64Util.decode(data)));
    }

    public static String desEncode(String data) {
        return Base64Util.encode(D_E_S.encrypt(data));
    }

    public static String desDecode(String data) {
        return StringUtil.fromBytes(D_E_S.decrypt(Base64Util.decode(data)));
    }

    public static String md5(String data) {
        return SecureUtil.md5(data);
    }

    public static String md5(File file) {
        return SecureUtil.md5(file);
    }

    public static String md5(InputStream is) {
        return SecureUtil.md5(is);
    }

    public static String sha1(String data) {
        return SecureUtil.sha1(data);
    }

    public static String sha1(File file) {
        return SecureUtil.sha1(file);
    }

    public static String sha1(InputStream is) {
        return SecureUtil.sha1(is);
    }
}
